<template>
  <a-drawer
    title="查看报价"
    width="90%"
    :visible="visible"
    @close="
      () => {
        this.visible = false
      }
    "
  >
    <a-card :bordered="false">
      <!-- <div class="table-operator">
        <a-button v-if="hasPerm('Quot_OfferSheetMain1.Add')" type="primary" icon="plus" @click="handleAdd()"
          >新建</a-button
        >
        <a-button
          v-if="hasPerm('Quot_OfferSheetMain1.Delete')"
          type="primary"
          icon="minus"
          @click="handleDelete(selectedRowKeys)"
          :disabled="!hasSelected()"
          :loading="loading"
          >删除</a-button
        >
        <a-button type="primary" icon="redo" @click="getDataList()">刷新</a-button>
      </div> -->

      <!-- <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="10">
            <a-col :md="4" :sm="24">
              <a-form-item label="查询类别">
                <a-select allowClear v-model="queryParam.condition">
                  <a-select-option key="DocumentNo">报价单编号</a-select-option>
                  <a-select-option key="CustomerName">客户名称</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-form-item>
                <a-input v-model="queryParam.keyword" placeholder="关键字" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-button
                type="primary"
                @click="
                  () => {
                    this.pagination.current = 1
                    this.getDataList()
                  }
                "
                >查询</a-button
              >
              <a-button style="margin-left: 8px" @click="repossess">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div> -->

      <a-table
        ref="table"
        :scroll="{ y: 530 }"
        :columns="columns"
        :rowKey="(row, index) => row.Id"
        :dataSource="data"
        :pagination="pagination"
        :loading="loading"
        @change="handleTableChange"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, getCheckboxProps: CheckboxProps }"
        :bordered="true"
        size="small"
      >
        <template slot="ApprovalStatus" slot-scope="text">
          <a-tag color="green" v-if="text == 1"> 审批通过 </a-tag>
          <a-tag color="blue" v-if="!text">
            未审批
          </a-tag>
          <a-tag color="red" v-if="text == 2">
            审批驳回
          </a-tag>
        </template>
        <span slot="CreateTime" slot-scope="CreateTime">{{ timeFormat(CreateTime) }}</span>
        <span slot="action" slot-scope="text, record">
          <template>
            <a v-if="record.ApprovalStatus == 1 && hasPerm('Quot_OfferSheetMain1.Print')" @click="Print(record.Id)"
              >打印</a
            >
            <a-divider v-if="record.ApprovalStatus == 1" type="vertical" />
            <a v-if="hasPerm('Quot_OfferSheetMain1.Edit') && !record.ApprovalStatus" @click="handleEdit(record.Id)"
              >编辑</a
            >
            <a-divider
              v-if="
                hasPerm('Quot_OfferSheetMain1.Edit') && hasPerm('Quot_OfferSheetMain1.Delete') && !record.ApprovalStatus
              "
              type="vertical"
            />
            <a v-if="record.ApprovalStatus" @click="Check(record.Id)">查看</a>
            <a-divider v-if="record.ApprovalStatus == 2" type="vertical" />
            <a v-if="record.ApprovalStatus == 2" @click="handleRevise(record.Id)">修改</a>
            <a
              v-if="hasPerm('Quot_OfferSheetMain1.Delete') && !record.ApprovalStatus"
              @click="handleDelete([record.Id])"
              >删除</a
            >
          </template>
        </span>
      </a-table>

      <edit-form ref="editForm" :disabled="disabled" :parentObj="this"></edit-form>
      <!-- 打印 -->
      <Print ref="Print" :parentObj="this"></Print>
    </a-card>
  </a-drawer>
</template>

<script>
const toThousands = function(data) {
  if (data != null) {
    return data.toFixed(2).replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,')
  } else {
  }
}
import EditForm from '../../../QuotationManagement/MyPrice/EditForm.vue'
import OperatorCache from '@/utils/cache/OperatorCache'
//打印
import Print from '../../../QuotationManagement/MyPrice/Print.vue'
import moment from 'moment'
const columns = [
  { title: '报价单编号', dataIndex: 'DocumentNo', width: '9%' },
  { title: '客户名称', dataIndex: 'CustomerName', width: '16%' },

  { title: '销售经理', dataIndex: 'AccountManager', width: '8%' },
  // { title: '交易类型', dataIndex: 'TransactionType' },
  // { title: '开票类型', dataIndex: 'BillingType' },

  {
    title: '折扣%',
    dataIndex: 'Discount',
    align: 'center',
    customRender: value => {
      if (value) return value + '%'
    },
    width: '80px'
  },
  {
    title: '金额合计(元)',
    dataIndex: 'TotalAmount',
    align: 'right',
    customRender: toThousands,
    width: '120px'
  },
  {
    title: '税金(元)',
    dataIndex: 'Taxes',
    align: 'right',
    customRender: toThousands,
    width: '120px'
  },
  // { title: '运输方式', dataIndex: 'TransportType' },
  { title: '送货地址', dataIndex: 'DeliveryAddress', width: '17%', ellipsis: true },
  {
    title: '运费(元)',

    dataIndex: 'Freight',
    align: 'right',
    customRender: toThousands,
    width: '100px'
  },
  { title: '联系人', dataIndex: 'LinkMan', width: '100px' },
  {
    title: '审批状态',
    dataIndex: 'ApprovalStatus',
    align: 'center',
    scopedSlots: { customRender: 'ApprovalStatus' },
    width: '100px'
  },
  // { title: '备注', dataIndex: 'Remark' },

  { title: '操作', dataIndex: 'action', align: 'left', scopedSlots: { customRender: 'action' }, width: '10%' }
]

export default {
  components: {
    EditForm,
    Print
  },

  data() {
    return {
      disabled: false,
      visible: false,
      data: [],
      name: '',
      Id: '', //当前登录人Id
      pagination: {
        current: 1,
        pageSize: 25,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['25', '50', '100'], // 设置pageSize的可选值，页面可以通过下拉框进行选择
        showSizeChanger: true
      },
      filters: {},
      CreatorId: this.Id,
      sorter: { field: 'Id', order: 'desc' },
      loading: false,
      columns,
      queryParam: {
        condition: 'CustomerName'
      },
      selectedRowKeys: []
    }
  },
  methods: {
    openForm(name, title) {
      this.title = title
      this.visible = true
      this.data = []
      this.name = name
      this.queryParam.condition = 'CustomerName'
      this.queryParam.keyword = name
      this.getDataList()
    },
    CheckboxProps(record) {
      return {
        props: {
          disabled: record.ApprovalStatus == 1
        }
      }
    },
    handleRevise(id) {
      this.disabled = false
      this.$refs.editForm.openForm(id, '修改')
    },
    repossess() {
      this.queryParam = { condition: 'CustomerName' }
      this.queryParam.keyword = this.name
      this.getDataList()
    },
    handleTableChange(pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }

      this.getDataList()
    },
    op() {
      return OperatorCache.info
    },
    getDataList() {
      this.selectedRowKeys = []
      this.Id = this.op().Id
      this.loading = true
      this.$http
        .post('/QuotationManagement/Quot_OfferSheetMain/GetDataListByMoreCondition', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order, DeptId: this.DeptId,
          Search: {
            Condition: this.queryParam.condition,

            Keyword: this.queryParam.keyword
          }
        })
        .then(resJson => {
          // //console.log(this.op())
          this.loading = false
          this.data = resJson.Data
          if (this.data) {
            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          }
        })
    },
    Print(id) {
      this.$refs.Print.openForm(id, '打印')
    },
    onSelectChange(selectedRowKeys) {
      //console.log(selectedRowKeys)
      this.selectedRowKeys = selectedRowKeys
    },
    hasSelected() {
      return this.selectedRowKeys.length > 0
    },
    handleAdd() {
      this.disabled = false
      this.$refs.editForm.openForm(null, '新建我的报价信息')
    },
    handleEdit(id) {
      this.disabled = false
      this.$refs.editForm.openForm(id, '编辑我的报价信息')
    },
    Check(id) {
      this.disabled = true
      this.$refs.editForm.openForm(id, '查看')
    },
    handleDelete(ids) {
      var thisObj = this
      this.$confirm('确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return new Promise((resolve, reject) => {
          thisObj.$http.post('/QuotationManagement/Quot_OfferSheetMain/DeleteDataOfAll', ids).then(resJson => {
            resolve()

            if (resJson.Success) {
              thisObj.$message.success('操作成功!')

              thisObj.getDataList()
            } else {
              thisObj.$message.error(resJson.Msg)
            }
          })
        })
      })
    },
    timeFormat(val) {
      // 时间格式化函数
      return moment(val).format('YYYY-MM-DD HH:mm')
    }
  }
}
</script>
